home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / dfpp01.zip / SCROLBAR.CPP < prev    next >
C/C++ Source or Header  |  1992-11-21  |  4KB  |  175 lines

  1. // ------------- scrolbar.cpp
  2.  
  3. #include "desktop.h"
  4. #include "textbox.h"
  5. #include "scrolbar.h"
  6.  
  7. ScrollBar::ScrollBar(BarPlane Plane, TextBox *par)
  8.                         : DFWindow(1, 1, par)
  9. {
  10.     windowtype = ScrollbarWindow;
  11.     if (par == NULL)
  12.         return;
  13.     scrollbox = 1;
  14.     sliding = False;
  15.     SetClearChar(SCROLLBARCHAR);
  16.     plane = Plane;
  17.     if (plane == HORIZONTAL)    {
  18.         rect.Left() = par->Left()+1;
  19.         rect.Right() = par->Right()-1;
  20.         rect.Top() = rect.Bottom() = par->Bottom();
  21.     }
  22.     else    {
  23.         rect.Left() = rect.Right() = par->Right();
  24.         rect.Top() = par->Top()+1;
  25.         rect.Bottom() = par->Bottom()-1;
  26.     }
  27.     SetColors();
  28. }
  29.  
  30. // -------- set the fg/bg colors for the window 
  31. void ScrollBar::SetColors()
  32. {
  33.     if (parent)    {
  34.         colors.fg = parent->FrameFG();
  35.         colors.bg = parent->FrameBG();
  36.     }
  37. }
  38.  
  39. Bool ScrollBar::SetFocus()
  40. {
  41.     if (parent != NULL)
  42.         parent->SetFocus();
  43.     return True;
  44. }
  45.  
  46. void ScrollBar::Paint()
  47. {
  48.     if (visible)    {
  49.         int fg = colors.fg;
  50.         int bg = colors.bg;
  51.         DFWindow::Paint();
  52.         if (plane == HORIZONTAL)    {
  53.             WriteWindowChar(LEFTSCROLLBOX, 0, 0, fg, bg);
  54.             WriteWindowChar(RIGHTSCROLLBOX, Width()-1, 0, fg, bg);
  55.             WriteWindowChar(SCROLLBOXCHAR, scrollbox, 0, fg, bg);
  56.         }
  57.         else    {
  58.             WriteWindowChar(UPSCROLLBOX, 0, 0, fg, bg);
  59.             WriteWindowChar(DOWNSCROLLBOX, 0, Height()-1, fg, bg);
  60.             WriteWindowChar(SCROLLBOXCHAR, 0, scrollbox, fg, bg);
  61.         }
  62.     }
  63. }
  64.  
  65. void ScrollBar::LeftButton(int mx, int my)
  66. {
  67.     if (parent != NULL && !sliding)    {
  68.         TextBox &Par = *(TextBox *)parent;
  69.         if (plane == VERTICAL)    {
  70.             // -- test for hitting the vertical scroll buttons
  71.             if (my == rect.Top())
  72.                 Par.ScrollDown();
  73.             else if (my == rect.Bottom())
  74.                 Par.ScrollUp();
  75.             // ------- test for hitting the vertical scroll box
  76.             else if (my-rect.Top() == scrollbox)    {
  77.                 sliding = True;
  78.                 desktop.mouse().SetTravel(rect.Left(), rect.Right(),
  79.                                 rect.Top()+1, rect.Bottom()-1);
  80.             }
  81.             else    {
  82.                 // ----- hit in the scroll bar
  83.                 if (my-rect.Top() < scrollbox)
  84.                     Par.PageUp();
  85.                 else
  86.                     Par.PageDown();
  87.             }
  88.         }
  89.         else    {
  90.             // -- test for hitting the horizontal scroll buttons
  91.             if (mx == rect.Left())
  92.                 Par.ScrollRight();
  93.             else if (mx == rect.Right())
  94.                 Par.ScrollLeft();
  95.             // ------- test for hitting the horizontal scroll box
  96.             else if (mx-rect.Left() == scrollbox)    {
  97.                 sliding = True;
  98.                 desktop.mouse().SetTravel(rect.Left()+1, rect.Right()-1,
  99.                                 rect.Top(), rect.Bottom());
  100.             }
  101.             else    {
  102.                 // ----- hit in the scroll bar
  103.                 if (mx-rect.Left() < scrollbox)
  104.                     Par.PageLeft();
  105.                 else
  106.                     Par.PageRight();
  107.             }
  108.         }
  109.     }
  110. }
  111.  
  112. void ScrollBar::MouseMoved(int mx, int my)
  113. {
  114.     if (sliding)    {
  115.         if (plane == VERTICAL)
  116.             MoveScrollBox(my - rect.Top());
  117.         else
  118.             MoveScrollBox(mx - rect.Left());
  119.     }
  120. }
  121.  
  122. void ScrollBar::ButtonReleased(int, int)
  123. {
  124.     if (sliding && parent != NULL)    {
  125.         TextBox &Par = *(TextBox *)parent;
  126.         desktop.mouse().SetTravel(0, desktop.screen().Width()-1, 0, desktop.screen().Height()-1);
  127.         int pct = (scrollbox-1)*100;
  128.         if (plane == VERTICAL)    {
  129.             pct /= (rect.Height()-2);
  130.             Par.VerticalPagePosition(pct);
  131.         }
  132.         else    {
  133.             pct /= (rect.Width()-2);
  134.             Par.HorizontalPagePosition(pct);
  135.         }
  136.     }
  137.     sliding = False;
  138. }
  139.  
  140. void ScrollBar::ParentSized(int xdif, int ydif)
  141. {
  142.     if (plane == HORIZONTAL)    {
  143.         Size(Right()+xdif, Top());
  144.         Move(Left(), Bottom()+ydif);
  145.     }
  146.     else    {
  147.         Size(Left(), Bottom()+ydif);
  148.         Move(Right()+xdif, Top());
  149.     }
  150. }
  151.  
  152. void ScrollBar::TextPosition(int TxPct)
  153. {
  154.     int len = plane == HORIZONTAL ? Width()-2 : Height()-2;
  155.     int sb = 1 + ((len * TxPct) / 100);
  156.     MoveScrollBox(sb);
  157. }
  158.  
  159. void ScrollBar::MoveScrollBox(int sb)
  160. {
  161.     if (sb != scrollbox)    {
  162.         int fg = colors.fg;
  163.         int bg = colors.bg;
  164.         int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
  165.         if (plane == HORIZONTAL)
  166.             x1 = sb, x2 = scrollbox;
  167.         else
  168.             y1 = sb, y2 = scrollbox;
  169.         WriteWindowChar(SCROLLBOXCHAR, x1, y1, fg, bg);
  170.         WriteWindowChar(SCROLLBARCHAR, x2, y2, fg, bg);
  171.         scrollbox = sb;
  172.     }
  173. }
  174.  
  175.